package com.buss.core.tag;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.buss.core.model.Sysmenu;
import com.buss.core.service.SysmenuService;

public class NavigationBar extends TagSupport {

	private static final long serialVersionUID = 1L;

	/** 菜单ID */
	private Long menuId;

	public int doStartTag() throws JspException {
		return SKIP_BODY;
	}

	public int doEndTag() throws JspException {
		WebApplicationContext context = WebApplicationContextUtils
				.getRequiredWebApplicationContext(pageContext
						.getServletContext());
		SysmenuService menuService = context.getBean(SysmenuService.class);
		//Sysmenu menu = menuService.loadSysMenu(menuId);
		List<Sysmenu> menus = menuService.querySysmenuByChild(getMenuId());
		
		JspWriter writer = pageContext.getOut();
		try {
			String navText = "企业管理平台 >> ";
			for(int i = (menus.size() - 1);i >= 0;i--){
				if(i > 0){
					navText += menus.get(i).getName() + " >> ";
				}else{
					navText += menus.get(i).getName();
				}
			}
			writer.write(navText);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	public Long getMenuId() {
		return menuId;
	}

	public void setMenuId(Long menuId) {
		this.menuId = menuId;
	}

}
